package com.yuyou.fn.portal.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.portal.service.IOuterCourseClassService;
import com.yuyou.fn.portal.vo.RecommendRecordDetailVo;
import com.yuyou.fn.portal.vo.SimpleCourseClass;
import org.apache.commons.lang3.Validate;
import org.springframework.stereotype.Service;

import com.yuyou.fn.portal.mapper.RecommendRecordMapper;
import com.yuyou.fn.portal.entity.RecommendRecord;
import com.yuyou.fn.portal.service.IRecommendRecordService;
import com.github.wens.framework.service.impl.SuperServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 *
 * RecommendRecord 表数据服务层接口实现类
 *
 */
@Service
public class RecommendRecordServiceImpl extends SuperServiceImpl<RecommendRecordMapper, RecommendRecord,Long> implements IRecommendRecordService {

    @Resource
    private IOuterCourseClassService outerCourseClassService;

    @Override
    public void addRecommendRecord(long userId, int userType, long classId) {
        RecommendRecord check = this.findRecommendRecordByUserIdClassId(userId, classId);
        if (check!=null){
            throw new BusException("推荐单已存在该课程");
        }
        RecommendRecord recommendRecord=new RecommendRecord();
        recommendRecord.setClassId(classId);
        recommendRecord.setUserId(userId);
        recommendRecord.setUserType(userType);
        recommendRecord.setCreateTime(TimeUtils.now());
        recommendRecord.setUpdateTime(TimeUtils.now());
        this.insert(recommendRecord);
    }

    @Override
    public void deleteRecommendRecordByUserIdRecommendIds(long userId, List<Long> recommendIds) {
        Validate.notNull(recommendIds);
        Example<RecommendRecord> example = RecommendRecord.createExample();
        example.createCriteria().andIn("recommendId",recommendIds).andEqualTo("userId",userId);
        this.deleteByExample(example);
    }

    @Override
    public List<RecommendRecord> findRecommendRecordListByUserId(long userId) {
        Example<RecommendRecord> example = RecommendRecord.createExample();
        example.createCriteria().andEqualTo("userId",userId);
        return this.findListByExample(example);
    }

    @Override
    public Long countRecommendRecordByUserId(long userId) {
        Example<RecommendRecord> example = RecommendRecord.createExample();
        example.createCriteria().andEqualTo("userId",userId);
        return this.countByExample(example);
    }

    @Override
    public RecommendRecord findRecommendRecordByUserIdClassId(long userId, long classId) {
        Example<RecommendRecord> example = RecommendRecord.createExample();
        example.createCriteria().andEqualTo("userId",userId).andEqualTo("classId",classId);
        return this.findOneByExample(example);
    }

    @Override
    public List<RecommendRecordDetailVo> findRecommendRecordDetailList(long userId) {
        List<RecommendRecord> recommendRecords = this.findRecommendRecordListByUserId(userId);
        List<Long> classIds = BeanUtils.toList(recommendRecords, "classId");
        if (Utils.checkListEmpty(classIds)){
            return Collections.EMPTY_LIST;
        }
        List<SimpleCourseClass> simpleCourseClassList=outerCourseClassService.findSimpleCourseClassByClassIds(classIds);
        Map<Long, SimpleCourseClass> classMap = BeanUtils.toMap(simpleCourseClassList, "classId");
        List<RecommendRecordDetailVo> recommendRecordDetailVoList=new ArrayList<>(recommendRecords.size());
        for (RecommendRecord recommendRecord:recommendRecords){
            RecommendRecordDetailVo recommendRecordDetailVo=new RecommendRecordDetailVo();
            SimpleCourseClass simpleCourseClass=classMap.get(recommendRecord.getClassId());
            BeanUtils.copyProperties(recommendRecordDetailVo,simpleCourseClass);
            recommendRecordDetailVo.setRecommendId(recommendRecord.getRecommendId());
            recommendRecordDetailVoList.add(recommendRecordDetailVo);
        }
        return recommendRecordDetailVoList;
    }
}